package blipappeal;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.wave.api.*;


// utility class to do appeal score evaluation
public class Score {

	// these are the key values that the gadget must set in its properties
	public static final String ONE = "one";
	public static final String TWO = "two";
	public static final String THREE = "three";
	public static final String FOUR = "four";
	public static final String FIVE = "five";

	/*
	 * get the cumulative average rating score for the blip
	 * @param blip - blip with scores in its properties
	 * @param gadgetUrl - Url of the gadget that is generating the scores
	 * @return average score or 0 if there are no ratings
	 */
	public static int getBlipScore(Blip blip, String gadgetUrl) {
		int cumAvg = 0;

        //TODO proper error handling
        assert(blip.isDocumentAvailable());
        TextView textView = blip.getDocument();
        GadgetView gadgetView = textView.getGadgetView();

        if ((gadgetView == null) || (gadgetView.getGadget(gadgetUrl) == null)) {
        	return 0;
        }
        
	    Gadget gadget = textView.getGadgetView().getGadget(gadgetUrl);
    	Map<String,Object> state = gadget.getProperties();
    	// get the state from the gadget
    	Set<Map.Entry<String,Object>> stateSet = state.entrySet();
    	//TODO temporary -- for dumping state to log
    	StringBuilder entries = new StringBuilder("Blips state: \n");
    	int cumCount=0;
    	int numVotes=0;
    	// walk through the state map looking for the counts
    	for(Map.Entry<String,Object> entry: stateSet) {
    		//TODO log code to dump the entire state
    		entries.append(entry.getKey() + ": " + (String)entry.getValue() + "\n");
    		
    		if (entry.getKey().equalsIgnoreCase(ONE)) {
    			int howmany = Integer.parseInt((String)entry.getValue());
    			cumCount+=howmany;
    			numVotes+=howmany;
    		}
    		else if (entry.getKey().equalsIgnoreCase(TWO)) {
    			int howmany = Integer.parseInt((String)entry.getValue());
    			cumCount += howmany * 2;
    			numVotes += howmany;
    		}
    		else if (entry.getKey().equalsIgnoreCase(THREE)) {
    			int howmany = Integer.parseInt((String)entry.getValue());
    			cumCount += howmany * 3;
    			numVotes += howmany;
    		}
    		else if (entry.getKey().equalsIgnoreCase(FOUR)) {
    			int howmany = Integer.parseInt((String)entry.getValue());
    			cumCount += howmany * 4;
    			numVotes += howmany;
    		}
    		else if (entry.getKey().equalsIgnoreCase(FIVE)) {
    			int howmany = Integer.parseInt((String)entry.getValue());
    			cumCount += howmany * 5;
    			numVotes += howmany;
    		}    			
    	}

    	if (numVotes > 0) {
    		cumAvg = cumCount/numVotes;
    	}
    	//TODO clean up debug logging
    	System.err.println(entries);
    	System.err.println("cumAvg = " + cumAvg);
			
	
	return cumAvg;
	}
	
	/*
	 * increase the visibility of high scoring blips
	 * @param blip - the blip to modify
	 * @param score - the blip's average rating
	 */
	
	public static void emboldenBlip(Blip blip, int score) {
		System.err.println("emboldening blip with score " + score);
		// number of leading characters to embolden
		int numChars = 0;
		// the style type to apply to the leading characters of the blip
		StyleType style = null;
		if (score == 5) {
			//everyone gave it the top score
			numChars = 40;
			style = StyleType.HEADING4;
		}
		else if (score == 4) {
			numChars = 20;
			style = StyleType.HEADING2;
		}

	
		if (numChars > 0) {
			 TextView textView = blip.getDocument();
			/*
			//set the text of the blip
			 String txt = textView.getText();
			 StyledText st = new StyledText(txt.substring(0, numChars), style);
			 // set the new styled beginning of the text
			 textView.replaceStyledText(new Range(0, numChars), st);
			 StyledText st = new StyledText("This is a great blip!", StyleType.HEADING1);
			 textView.appendStyledText(st);
			 */
			 
			
		}
			
		
	}


}
